package com.oa.modules.approvalsec1.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.oa.common.utils.AppUtils;
import com.oa.common.utils.PageUtils;
import com.oa.common.utils.R;
import com.oa.modules.approvalsec1.entity.NewsNoticeEntity;
import com.oa.modules.approvalsec1.service.NewsNoticeService;
import com.oa.modules.sys.controller.AbstractController;
import com.oa.modules.sys.service.SysUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.oa.modules.approvalsec1.entity.AssistInManagingEntity;
import com.oa.modules.approvalsec1.service.AssistInManagingService;


/**
 * 帮办申请
 *
 * @author jiangwei
 * @email 785297226@qq.com
 * @date 2021-03-22 10:03:27
 */
@RestController
@RequestMapping("assist/in/managing")
@Api(tags = "帮办申请相关接口")
public class AssistInManagingController extends AbstractController {
    @Autowired
    private AssistInManagingService assistInManagingService;

    @Autowired
    private NewsNoticeService newsNoticeService;

    @Autowired
    private SysUserRoleService userRoleService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = assistInManagingService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "信息")
    public R info(@PathVariable("id") String id) {
        AssistInManagingEntity assistInManaging = assistInManagingService.getById(id);

        return R.ok().put("assistInManaging", assistInManaging);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存")
    public R save(@RequestBody AssistInManagingEntity assistInManaging) {
        Map result = assistInManagingService.attacheApplication(assistInManaging);
        if ((Double)result.get("code") != 0) {
            return R.error((String) result.get("msg"));
        } else {
            String id = AppUtils.getUUID();
            assistInManaging.setId(id);
            assistInManaging.setCreateDate(new Date());
            assistInManagingService.save(assistInManaging);
            NewsNoticeEntity noticeEntity = new NewsNoticeEntity();
            noticeEntity.setId(AppUtils.getUUID());
            noticeEntity.setCreateDate(new Date());
            noticeEntity.setMatterId(id);
            noticeEntity.setMatterType("0");
            List<Long> userIds = userRoleService.queryRoleIdList(7L);
            userIds.forEach(item->{
                noticeEntity.setPushUserId(item.toString());
                newsNoticeService.save(noticeEntity);
            });
            return R.ok();
        }
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update(@RequestBody AssistInManagingEntity assistInManaging) {
        assistInManaging.setUpdateDate(new Date());
        assistInManagingService.updateById(assistInManaging);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public R delete(@RequestBody String[] ids) {
        assistInManagingService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 发送手机验证码
     *
     * @param phoneNumber
     * @return
     */
    @PostMapping("/sendSms")
    public R sendSms(@RequestParam("phoneNumber") String phoneNumber) {
        assistInManagingService.sendSms(phoneNumber);
            return R.ok("验证发发送成功");
    }

    @PostMapping("/getArea")
    public R getArea() {
        return R.ok().put("data", assistInManagingService.getArea());
    }


}
